package servlet.stu;

import bean.Public;
import bean.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import dao.UserDaoImpl;
import utils.JwtUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;


//学生预约公共设施
@WebServlet("/stu/appointmentPublic")
public class AppointmentPublicServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String user_token = request.getHeader("user_token_header");
        Integer stuId = JwtUtils.getStuIdFromToken(user_token,"stuId");
        String stuName = JwtUtils.getStuNameFromToken(user_token,"stuName");

        Integer publicTime= Integer.valueOf(request.getParameter("publicTime"));
        Integer publicId= Integer.valueOf(request.getParameter("publicId"));
        Object data= null;
        try {
            data = UserDaoImpl.appointmentPublic(publicTime,publicId,stuId,stuName);
        } catch (SQLException e) {
            throw new RuntimeException(e);

        }
        Result result = new Result();
        if(data!=null){
            result.success(data);
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);


        }else{
            result.fail();
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);


        }
    }
}
